//
//  VOHMusicViewController.m
//  VoiceOfHeart
//
//  Created by 吉龙 on 16/8/12.
//  Copyright © 2016年 吉龙. All rights reserved.
//

#import "VOHMusicViewController.h"
#import "VOHMusicMainView.h"
#import "VOHMusicCollectionViewCell.h"
#import "VOHMusicTableViewCell.h"
#import "VOHMusicHeader.h"
#import "VOHMusicScrollModel.h"
#import "VOHMusicCollectionModel.h"
#import "VOHMusicTableViewModel.h"
#import "NetHandler.h"
#import "VOHTableHeaderView.h"
#import "VOHMoreViewController.h"
#import "VOHMusicNavigationBar.h"
#import "VOHMusicDetailViewController.h"
@interface VOHMusicViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)VOHMusicMainView *mainView;
@property(nonatomic, strong)NSMutableArray *scrollArray;
@property(nonatomic, strong)NSMutableArray *scrollImageUrlArr;
@property(nonatomic, strong)NSMutableArray *collectionArray;
@property(nonatomic, strong)NSMutableArray *tableArray;
@property(nonatomic, strong)NSMutableArray *table2Array;
@property(nonatomic, assign)NSIndexPath *currentIndexPath;
@property(nonatomic, strong)NSString *currentCategory;
@end

@implementation VOHMusicViewController

-(void)loadView{
    self.mainView = [[VOHMusicMainView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.mainView ;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    VOHMusicNavigationBar *bar = (VOHMusicNavigationBar *)self.navigationController.navigationBar;
    __weak typeof(self)weakSelf = self;
    bar.backBtn.block = ^(VOHButton *sender){
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
    };

    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self addCollectionView];
    [self addTableView];
    [self handle];
    self.currentIndexPath = [NSIndexPath indexPathForItem:0 inSection:0];
    self.title = @"推荐";
}
-(void)addCollectionView{
    self.mainView.topView.collectionView.delegate = self;
    self.mainView.topView.collectionView.dataSource = self;
    [self.mainView.topView.collectionView registerClass:[VOHMusicCollectionViewCell class] forCellWithReuseIdentifier:@"music"];
}
-(void)addTableView{
    self.mainView.tableView.tableView.delegate = self;
    self.mainView.tableView.tableView.dataSource = self;
    
    [self.mainView.tableView.tableView registerClass:[VOHMusicTableViewCell class] forCellReuseIdentifier:@"tableView"];
    
    [self.mainView.tableView.tableView registerClass:[VOHMusicHeader class] forHeaderFooterViewReuseIdentifier:@"header"];
    self.mainView.tableView.tableView.showsVerticalScrollIndicator = NO;
    
    self.mainView.tableView2.delegate =self;
    self.mainView.tableView2.dataSource = self;
    [self.mainView.tableView2 registerClass:[VOHMusicTableViewCell class] forCellReuseIdentifier:@"tableView"];
    self.mainView.tableView2.showsVerticalScrollIndicator = NO;
}
#pragma mark -------collectionView协议

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 12;
}
-(VOHMusicCollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    VOHMusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"music" forIndexPath:indexPath];
    if (self.currentIndexPath == indexPath) {
        cell.titleLabel.textColor = [UIColor jinjuse];
        cell.maskView.alpha = 1;
    }else{
        cell.titleLabel.textColor = [UIColor grayColor];
        cell.maskView.alpha = 0;
    }
    cell.title = self.collectionArray[indexPath.item];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.currentIndexPath = indexPath;
    [self.mainView.topView.collectionView reloadData];
    if (indexPath.item == 0) {
        [self.mainView.tableView2 removeFromSuperview];
        [self.mainView addSubview:self.mainView.tableView];
        [self.mainView.tableView.tableView setContentOffset:CGPointMake(0, 0)];
        [self.mainView.tableView.tableView reloadData];
        self.title = @"推荐";
    }else{
        [self handle2];
        [self.mainView.tableView removeFromSuperview];
        [self.mainView addSubview:self.mainView.tableView2];
        [self.mainView.tableView2 setContentOffset:CGPointMake(0, 0)];
        [self.mainView.tableView2 reloadData];
        self.currentCategory = self.collectionArray[indexPath.item];
        self.title = self.currentCategory;
        
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark---------UITableView协议

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.mainView.tableView.tableView == tableView) {
        return 5;
    }
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.mainView.tableView.tableView == tableView) {
        return 3;
    }
    return self.table2Array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    VOHMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableView"];
    if (self.mainView.tableView.tableView == tableView) {
        cell.model = [self.tableArray[indexPath.section + 1] list][indexPath.row];
    }else{
        cell.model = self.table2Array[indexPath.row];
    }
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (self.mainView.tableView.tableView == tableView) {
        VOHMusicHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
        VOHMusicTableViewModel *model = self.tableArray[section + 1];
        
        header.model = model;
        __weak typeof(self)weakSelf = self;
        header.button.block = ^(VOHButton *sender){
            VOHMoreViewController *moreCtr = [[VOHMoreViewController alloc] init];
            moreCtr.tagName = model.tagName;
            [weakSelf.navigationController pushViewController:moreCtr animated:YES];
        };
        return header;
    }
    return nil;
    }

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (self.mainView.tableView.tableView == tableView) {
        return 40;
    }else{
        return 0;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    VOHMusicDetailViewController *detailCtr = [[VOHMusicDetailViewController alloc] init];
    [self.navigationController pushViewController:detailCtr animated:YES];
}

#pragma mark ------加载数据
- (void)handle {
    [NetHandler getDataWithUrl:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=2&contentType=album&device=iPhone&scale=2&version=4.3.20" completion:^(NSData *data) {
        if (data != nil) {
            NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSDictionary *tagsDic = [rootDic objectForKey:@"tags"];
            NSArray *tagsArray = [tagsDic objectForKey:@"list"];
            self.collectionArray = [NSMutableArray arrayWithObject:@"推荐"];
            for (NSDictionary *dic in tagsArray) {
                VOHMusicCollectionModel *titleList = [[VOHMusicCollectionModel alloc]init];
                [titleList setValuesForKeysWithDictionary:dic];
                [self.collectionArray addObject:titleList.tname];
            }
            
            self.scrollArray = [NSMutableArray array];
            self.scrollImageUrlArr = [NSMutableArray array];
            NSDictionary *focusDictionary = [rootDic objectForKey:@"focusImages"];
            NSArray *focusArray = [focusDictionary objectForKey:@"list"];
            for (NSDictionary *dic in focusArray) {
                VOHMusicScrollModel *imageList = [[VOHMusicScrollModel alloc]init];
                [imageList setValuesForKeysWithDictionary:dic];
                [self.scrollArray addObject:imageList];
                [self.scrollImageUrlArr addObject:imageList.pic];
                VOHTableHeaderView *header = (VOHTableHeaderView *)self.mainView.tableView.tableView.tableHeaderView;
                header.array = self.scrollImageUrlArr;

            }
            
            NSDictionary *categoryDic = [rootDic objectForKey:@"categoryContents"];
            NSArray *categoryArray = [categoryDic objectForKey:@"list"];
            self.tableArray = [NSMutableArray array];
            for (NSDictionary *dic in categoryArray) {
                VOHMusicTableViewModel *categoryMusic = [[VOHMusicTableViewModel alloc]init];
                [categoryMusic setValuesForKeysWithDictionary:dic];
                [self.tableArray addObject:categoryMusic];
            }
            
            [self.mainView.tableView.tableView reloadData];
            [self.mainView.topView.collectionView reloadData];
            
//            [self.tableView headerEndRefreshing];
        }
    }];
}

static int page2 = 1;
- (void)handle2 {
    [NetHandler getDataWithUrl:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/category/album?calcDimension=hot&categoryId=2&device=iPhone&pageId=%d&pageSize=20&position=1&status=0&tagName=%@&title=精选歌单", page2, self.currentCategory] completion:^(NSData *data) {
        if (data != nil) {
            NSDictionary *rootDic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            if (self.table2Array.count == 0) {
                self.table2Array = [NSMutableArray array];
            }else if (page2 == 1) {
                [self.table2Array removeAllObjects];
            }
            NSArray *array = [rootDic objectForKey:@"list"];
            for (NSDictionary *dic in array) {
                VOHMusicDetailModel *programS = [[VOHMusicDetailModel alloc]init];
                [programS setValuesForKeysWithDictionary:dic];
                [self.table2Array addObject:programS];
            }
            [self.mainView.tableView2 reloadData];
        }
    }];
}





@end
